package com.maschinenstuermer.profiler.trace.presentation;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.TreeItem;

/**
 * Paints an item {@link org.eclipse.swt.widgets.Item} as a percentage bar.
 * 
 * @author km
 */
public class PercentItemPainter implements Listener {
	private final Set<Integer> percentColumnIndexes;

	/**
	 * @param percentColumnIndexes columns to paint as percentage bar
	 */
	public PercentItemPainter(Integer[] percentColumnIndexes) {
		List<Integer> asList = Arrays.asList(percentColumnIndexes);
		this.percentColumnIndexes = new HashSet<Integer>(asList);
	}

	@Override
	public void handleEvent(Event event) {
		if (event.item instanceof TreeItem) {
			final TreeItem treeItem = (TreeItem) event.item;
			handleEventTreeItem(event, treeItem);
		} else if (event.item instanceof TableItem) {
			final TableItem tableItem = (TableItem) event.item;
			handleEventTableItem(event, tableItem);
		} else {
			throw new IllegalArgumentException("Illegal event item of type "
					+ event.item.getClass() + "!");
		}
	}

	private void handleEventTableItem(Event event, TableItem tableItem) {
		final int index = event.index;
		if (percentColumnIndexes.contains(index)) {
			final int percent = getPercent(tableItem, index);
			final Rectangle bounds = getBounds(tableItem, index);
			drawPercent(event, index, percent, bounds);
		}
	}

	private void handleEventTreeItem(Event event, TreeItem treeItem) {
		final int index = event.index;
		if (percentColumnIndexes.contains(index)) {
			final int percent = getPercent(treeItem, index);
			final Rectangle bounds = getBounds(treeItem, index);
			drawPercent(event, index, percent, bounds);
		}
	}

	private void drawPercent(Event event, final int index, final int percent,
			final Rectangle bounds) {
		final GC gc = event.gc;
		final Color foreground = gc.getForeground();
		final Color background = gc.getBackground();
		
		final Display display = event.display;
		gc.fillRectangle(bounds);
		bounds.x += 2;
		bounds.y += 2;
		bounds.width -= 4;
		bounds.height -= 5;
		bounds.width = (bounds.width - 1) * percent / 100;
		gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
		gc.fillRectangle(bounds);

		gc.setForeground(background);
		gc.setBackground(foreground);
	}

	private Rectangle getBounds(TableItem item, final int index) {
		return item.getBounds(index);
	}

	private int getPercent(TableItem item, final int index) {
		return Integer.parseInt(item.getText(index));
	}

	private Rectangle getBounds(TreeItem item, final int index) {
		return item.getBounds(index);
	}

	private int getPercent(TreeItem item, final int index) {
		return Integer.parseInt(item.getText(index));
	}
}
